<?php

use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use kartik\file\FileInput;
use kartik\select2\Select2;
use yii\web\JsExpression;
use app\common\Functions;

/* @var $this yii\web\View */
/* @var $model \yii2mod\rbac\models\BizRuleModel */
/* @var $form ActiveForm */

$showForm = []; // 显示表格

//默认的字段类型
$defineFieldStyle = [
        'template'      => "{label}\n<div class=\"col-sm-8\">{input}</div>\n{error}",
        'labelOptions' => ['class' => 'col-sm-3 control-label']];



$defineFieldStyleThree = [
    'template'      => "{label}\n<div class=\"col-sm-6\">{input}</div>\n{error}",
    'labelOptions' => ['class' => 'col-sm-6 control-label']];

$defineFieldStyle3 = [
    'template'      => "{input}\n{error}",
    'labelOptions' => ['style'=>'display:block']];
//
//['labelOptions'=>['style'=>'display:none']]

$ownCustStyles = [];
if ($model->ownCusts )
{
    if ($model->ownCusts->cust_name )
    {
        if ($model->own_cust_id!=0)
        {
            $ownCustStyles['initValueText'] = $model->ownCusts->cust_name;
        }
    }
}
$ownCustStyles['options'] = ['placeholder' => '请选择 ...'];

$ownCustStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' => 'zh-CN',
//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachcust',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),
    'templateSelection' => new JsExpression('function (res) {   
                    return res.text; }')];

// 区域是否隐藏
$regionStyle='display:none';
if ($update)
{
    $regionStyle ='display:show';
}

$useCustStyles = [];
if ($model->useCusts->cust_name)
{
    $useCustStyles['initValueText'] = $model->useCusts->cust_name;
}

$useCustStyles['options'] = ['placeholder' => '请选择 ...'];

$useCustStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' => 'zh-CN',

//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachcust',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),
    'templateSelection' => new JsExpression('function (res) {             
                    return res.text; }')];

// 初始化员工类型样式
$orderStaffIDStyles = [];
$orderStaffIDStyles['options'] = ['placeholder' => '请选择 ...'];

$orderStaffIDStyles['pluginOptions'] = [
    'placeholder' => '查询 ...',
    'allowClear' => true,
    'minimumInputLength' => 1,
    'language' =>  'zh-CN',

//        [
//        'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//    ],
    'ajax' => [
        'url' => 'seachorderstaff',
        'dataType' => 'json',
        'data' => new JsExpression('function(params) { return {q:params.term}; }')
    ],
    'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
    'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),



    'templateSelection' => new JsExpression('function (res) { 
                    
                    if (res.id!=res.text)
                    {
                        $("input#prodinst-orderstaffid").val(res.id);
                    }
                    
                     
                    return res.text; }')];
//form-horizontal
?>

    <?php $form = ActiveForm::begin([
        'options'=>['class' => 'form-horizontal']
    ]); ?>
    <div class="box-body">

        <div style="display:none">
            <?php
            echo  $form->field($model, 'id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->id]);
            echo  $form->field($model, 'prod_spec_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prod_spec_id]);
            ?>
        </div>
        <?php
        // 如果是房源修改，需要进行父级产品的设置
        // 同时满足两个条件 一是创建房源非楼盘 而是不是从处跳转的& !$parent_prod_id
        if ($prod_spec_id==10002 ||  $prod_spec_id==10004 || $prod_spec_id==10001 )
        {
            $parentStyles=[];

            if ($model->parentProdName)
            {

                $parentStyles['initValueText'] = $model->parentProdName;

            }

            $parentStyles['options'] = ['placeholder' => '请选择 ...'];

            $parentStyles['pluginOptions'] = [
            'placeholder' => '查询 ...',
            'allowClear' => true,
            'minimumInputLength' => 2,
            'language' => 'zh-CN',

//                [
//                'errorLoading' => new JsExpression("function () { return '稍等...'; }"),
//            ],
            'ajax' => [
                'url' => 'seachprodinst',
                'dataType' => 'json',
                'data' => new JsExpression('function(params) { return {q:params.term,parent_prod_spec_id: ' . $parent_prod_spec_id . '}; }')
            ],
            'escapeMarkup' => new JsExpression('function (markup) { return markup; }'),
            'templateResult' => new JsExpression('function(res) { 
            
            return res.text; }'),



            'templateSelection' => new JsExpression('function (res) { 
                    
                    if (res.id!=res.text)
                    {
                        $("input#prodinst-parent_id").val(res.id);
                    }
                    
                     
                    return res.text; }')];

         echo $form->field($model, 'parent_id',$defineFieldStyle)->label(  '所属楼盘名称')->widget(Select2::classname(), $parentStyles);
        }

           // echo  $form->field($model, 'parent_id',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->parent_id]);
        ?>
       <?php  if ($prod_spec_id!=10001): ?>
            <?php  echo $form->field($model, 'use_cust_id',$defineFieldStyle)->label( '租户名称')->widget(Select2::classname(), $useCustStyles); ?>
       <?php endif; ?>

     <?php
         if($prod_spec_id!=10002)
         {
             echo  $form->field($model, 'prod_name',$defineFieldStyle)->label( $prodSpecName . '别名')->textInput(['maxlength' => 64]);
         }
     ?>
        <?php echo $form->field($model, 'own_cust_id',$defineFieldStyle)->label( '产权客户名称')->widget(Select2::classname(), $ownCustStyles); ?>

        <?php  if ($prod_spec_id!=10002): ?>
        <div   style="width:100% ;float: left;">
            <div class="col-sm-6" style="padding-left: 0px;">
            <?php
            // province
            // 如果类型是区域的话展示联动列表
//            echo $form->field($model,  'province', $defineFieldStyleThree)->label('省份')->dropDownList( $model->getCityList(['10'=>'福建']), [
//                'prompt'=>'--请选择市--',
//
//                'onchange'=>'
//                            $(".form-group #prodinst-city").show();
//                            $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
//                            $("select#prodinst-city").html(data);
//                            });',
//            ]);
            echo $form->field($model, "city", $defineFieldStyleThree)->label('地市')->textInput(['style'=>'display:block']) ->dropDownList( $model->getCityList(10),
                [
                    'prompt'=>'--请选择市--',

                    'onchange'=>'
                                    $(".form-group #prodinst-region_id").show();
                                    $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
                                        $("select#prodinst-region_id").html(data);
                                    });',
                ]);
            ?>
            </div>
            <div class="col-sm-5" style="padding-left: 0px;padding-right: 0px;">
<!--                <div class="col-sm-4 padding-right: 25px;" >-->
<!--                --><?php
//                echo $form->field($model, "city", $defineFieldStyle3)->label('地市')->textInput(['style'=>'display:']) ->dropDownList( $model->getCityList($model->province),
//                    [
//                        'prompt'=>'--请选择市--',
//                        'style'=>$regionStyle,
//                        'onchange'=>'
//                                    $(".form-group #prodinst-region_id").show();
//                                    $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
//                                        $("select#prodinst-region_id").html(data);
//                                    });',
//                    ]);
//                ?>
<!--                </div>-->

                <div class="col-sm-6"   style="padding-left: 25px;" >
                <?php // ['labelOptions'=>['style'=>'display:none']]
                echo $form->field($model, "region_id", $defineFieldStyle3 )->label('区县')->textInput(['style' => 'display:none;']) ->dropDownList( $model->getCityList($model->city),
                    [
                        'prompt'=>'--请选择区、县--',
                        'style'=>$regionStyle,
                        'onchange'=>'
                                $(".form-group #prodinst-region_name").show();
                                $.post("'.yii::$app->urlManager->createUrl('prod/prodinst/site').'?parent_region_id="+$(this).val(),function(data){
                                    $("select#prodinst-region_name").html(data);
                                });',
                    ]);
                ?>
                </div>
                <div class="col-sm-6"   style="padding-left: 25px;" >
                    <?php // ['labelOptions'=>['style'=>'display:none']]
                    echo $form->field($model, "region_name", $defineFieldStyle3 )->label('街道')->textInput(['style' => 'display:none;']) ->dropDownList( $model->getCityList($model->region_id),
                        [
                            'prompt'=>'--请选择街道--',
                            'style'=>$regionStyle,
                        ]);
                    ?>
                </div>
            </div>
         </div>
        <?php endif; ?>

        <?php if ($prod_spec_id==10002): ?>
            <?php // 楼盘状态 ?>

            <?php echo $form->field($model, 'state', $defineFieldStyle )->label('房源状态')->dropDownList($domainTypes); ?>

        <?php endif; ?>


        <?php  foreach ($attrSpecs as $attrSpec): ?>

            <?php $prodAttr = $attrSpec->attrSpecs;  ?>


            <?php // 文件 ?>
            <?php if ($prodAttr->attr_spec_type_id==2):?>
                <?php
                $pluginOptions=[];
                $pluginOptions['initialPreviewAsData'] = true;
                $pluginOptions['showRemove'] = true;
                $pluginOptions['showUpload'] = false;

                if ($update)
                {
                    if ($p1)
                    {
                        $pluginOptions['initialPreview'] = $p1[$prodAttr->attr_spec_id];
                    }
                }
                ?>

                <?php  echo $form->field($uploadModel, "file[{$prodAttr->attr_spec_id}][]",$defineFieldStyle)->label($prodAttr->attr_spec_name)->widget(FileInput::classname(), [
                    'options' => ['multiple' => false],
                    'pluginOptions' =>$pluginOptions,
                ])->fileInput();?>
                <?php // 如果type等于3 需要进行字符转义 ?>
            <?php elseif ($prodAttr->attr_spec_type_id==3): ?>
                <?php echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle )->label($prodAttr->attr_spec_name)->dropDownList(Functions::getDomains('attr_spec',$prodAttr->attr_spec_id)); ?>
            <?php else: ?>
                <?php echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][content]", $defineFieldStyle )->label($prodAttr->attr_spec_name)->textInput(['maxlength' => 64]); ?>
            <?php endif; ?>

            <div style="display:none">
                <?php echo $form->field($model, "localProdAttrsInsts[{$prodAttr->attr_spec_id}][attrSpecTypeID]",['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $prodAttr->attr_spec_type_id]); ?>
            </div>
        <?php endforeach; ?>

<!--        --><?php //if(!$update): ?>
<!---->
<!--            --><?php //echo  $form->field($model, 'orderStaffID',['labelOptions'=>['style'=>'display:none']])->hiddenInput(['value' => $model->orderStaffID]); ?>
<!---->
<!--            --><?php //if ($model->orderStaffName): ?>
<!--                --><?php //$orderStaffIDStyles['initValueText'] = $model->orderStaffName; ?>
<!--            --><?php //endif; ?>
<!---->
<!---->
<!--            --><?php //echo $form->field($model, 'orderStaffName',$defineFieldStyle)->label( '订单追踪人')->widget(Select2::classname(), $orderStaffIDStyles); ?>
<!---->
<!---->
<!---->
<!--            --><?php //echo  $form->field($model, 'orderRemark',$defineFieldStyle)->label( '订单描述')->textInput(['maxlength' => 64]); ?>
<!---->
<!---->
<!--        --><?php //endif; ?>




        <div class="box-footer">
            <?php echo Html::submitButton($model->getIsNewRecord() ? Yii::t('region', 'Create') : Yii::t('region', 'Update'), [
                'class' => $model->getIsNewRecord() ? 'btn btn-success' : 'btn btn-primary', ]); ?>
            <?php echo Html::a(Yii::t('region', 'Cancel'), Yii::$app->request->getReferrer(), ['class' => 'btn btn-default']); ?>
        </div>

    </div>
    <?php ActiveForm::end(); ?>

